<?php

/*
    商品浏览记录的处理
 */
class GoodsviewsModel extends BaseModel
{
    var $table  = 'goods_views';

    /**
     * @param $goods_id
     * 当商品被删除时应该清除对应的商品浏览记录
     */
    public function clearviews($goods_id)
    {
        $sql = "delete from ".$this->table." where goods_id='{$goods_id}' ";
        $this->query($sql);
    }

    /**
     * @param $goods_id
     * 添加一条记录
     * 要求记录不能重复
     *
     * 3=>pc来源
     */
    public function logview($goods_id,$source=3)
    {
        //找到用户id
        $mod = m('frm_log_action');
        $uid = $mod->getuserid();
        if(empty($uid))
        {
            return false;
        }
        //记录是否存在
        $where = "source='{$source}' and goods_id='{$goods_id}' and user_id='{$uid}' ";
        $sql = "select * from ".$this->table." where {$where}";
        $row = $this->getRow($sql);
        $time = time();
        if(empty($row)){


            $data = array();
            $data['goods_id'] = $goods_id;
            $data['source'] = $source;
            $data['user_id'] = $uid;
            $data['add_time'] = $time;
            $data['update_time'] = $time;
            $this->add($data);
        }else{
            //更新记录

            $sql = "update ".$this->table." set update_time='{$time}' where {$where} ";
            $this->query($sql);
        }

        return true;

    }

}

?>